package com.xc.sys.controller;

import com.google.common.collect.Lists;
import com.xc.common.object.LayuiTableResponse;
import com.xc.common.object.PageInfo;
import com.xc.common.object.TreeObject;
import com.xc.common.tool.Helper;
import com.xc.db.tables.SysDict;
import com.xc.sys.entity.SysDictEntity;
import com.xc.sys.service.SysDictService;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping({ "/dict" })
public class SysDictController {
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private SysDictService sysDictService;
	private SysDict DICT = SysDict.SYS_DICT.as("DICT");
	@CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.GET})
	@RequestMapping({ "/getDictTree" })
	public List<TreeObject> getDictTree(Integer pid) {
		List<TreeObject> list = new ArrayList<>();
		joinTreeNode(pid, list, null);
		return list;
	}

	private void joinTreeNode(Integer parentId, List<TreeObject> list, TreeObject node) {
		List<SysDictEntity> result = this.sysDictService.selectByParentId(parentId,DICT.ORDERNO.asc());
		for (SysDictEntity r : result) {
			Integer id = Integer.valueOf(r.getId());
			String name = r.getName();
			TreeObject treeObject = new TreeObject();
			treeObject.setName(name);
			treeObject.setId(id+"");
			List<SysDictEntity> result2 = this.sysDictService.selectByParentId(id);
			if (result2.size() != 0) {
				treeObject.setIsParent(true);
			}
			list.add(treeObject);
		}
	}
	@CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.GET})
	@RequestMapping({ "/getList" })
	public LayuiTableResponse<SysDictEntity> getDicts() {
		String parentId = request.getParameter("parentId");
		LayuiTableResponse<SysDictEntity> ltr = new LayuiTableResponse<>();
		int limit = Integer.valueOf(request.getParameter("limit"));
		int page = Integer.valueOf(request.getParameter("page"));
		List<SysDictEntity> list2 = new ArrayList<>();
		Condition cond = null;
		if (parentId != null) {
			cond = DICT.PARENT_ID.eq(Integer.valueOf(parentId));
			SysDictEntity entity = this.sysDictService.getEntityById(parentId);
			list2.add(entity);
		}else{
			cond = DICT.PARENT_ID.isNull();
		}
		Long count = this.sysDictService.count(cond);
		PageInfo<SysDictEntity> pageInfo = new PageInfo<>(limit, page);
		ltr.setCount(count+1);
		sysDictService.select(cond, pageInfo, DICT.ORDERNO.asc());
		List<SysDictEntity> list = pageInfo.getList();
		if (page == 1) {
			for (SysDictEntity nv : list) {
				list2.add(nv);
			}
		}
		ltr.setData(list2);
		return ltr;
	}
	@CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.GET})
	@RequestMapping({ "/getDictsByParentCode" })
	public LayuiTableResponse<SysDictEntity> getDictsByParentCode(HttpServletRequest request) {
		LayuiTableResponse<SysDictEntity> ltr = new LayuiTableResponse<>();
		String code = request.getParameter("code");
		List<SysDictEntity> list = sysDictService.select(DICT.CODE.eq(code),DICT.ORDERNO.asc());
		if(list.size()==0){
			return ltr;
		}
		String pid = list.get(0).getId();
		List<SysDictEntity> kids = sysDictService.select(DICT.PARENT_ID.eq(Integer.valueOf(pid)),DICT.ORDERNO.asc(), DICT.CODE.asc());
		ltr.setData(kids);
		return ltr;
	}
	@CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.GET})
	@RequestMapping({ "/getDictByCode" })
	public SysDictEntity getDictByCode(String parentCode, String code) {
		List<SysDictEntity> list = this.sysDictService.select(DICT.CODE.eq(parentCode),DICT.ORDERNO.asc());
		SysDictEntity pdict = new SysDictEntity();
		SysDictEntity dict = new SysDictEntity();
		if(StringUtils.isEmpty(code)) {
			return dict;
		}
		String[] codeArr = StringUtils.split(code,",");
		List<String> nameList = Lists.newArrayList();
		if(codeArr.length==0) {
			return dict;
		}
		if (list.size() == 1) {
			pdict = (SysDictEntity) list.get(0);
			list = sysDictService.select(DICT.CODE.eq(codeArr[0]).and(DICT.PARENT_ID.eq(Integer.valueOf(pdict.getId()))),DICT.ORDERNO.asc());
			if (list.size() == 1) {
				dict = (SysDictEntity) list.get(0);
				nameList.add(dict.getName());
			}
		}
		for(int i=1;i<codeArr.length;i++) {
			list = sysDictService.select(DICT.CODE.eq(codeArr[i]).and(DICT.PARENT_ID.eq(Integer.valueOf(pdict.getId()))),DICT.ORDERNO.asc());
			if (list.size() == 1) {
				dict = (SysDictEntity) list.get(0);
				nameList.add(dict.getName());
			}
		}
		dict.setName(StringUtils.join(nameList,","));
		return dict;
	}

	@RequestMapping({ "/modify" })
	public LayuiTableResponse<SysDictEntity> modify(SysDictEntity entity) {
		sysDictService.update(entity);
		LayuiTableResponse<SysDictEntity> ltr = new LayuiTableResponse<>();
		return ltr;
	}

	@RequestMapping({ "/move" })
	public LayuiTableResponse<SysDictEntity> move(String id, String parentId) {
		SysDictEntity record = sysDictService.getEntityById(id);
		record.setParentId(parentId);
		sysDictService.update(record);
		LayuiTableResponse<SysDictEntity> ltr = new LayuiTableResponse<>();
		return ltr;
	}

	@RequestMapping({ "/add" })
	public LayuiTableResponse<SysDictEntity> add(@RequestBody Map<String,String> params) {
		SysDictEntity entity = Helper.map2Bean(new SysDictEntity(), params);
		sysDictService.insert(entity);
		LayuiTableResponse<SysDictEntity> ltr = new LayuiTableResponse<>();
		return ltr;
	}

	@CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.POST})
	@RequestMapping({"/delete"})
	public LayuiTableResponse<SysDictEntity> delete(@RequestBody Map<String,String> params) {
		String idstr = params.get("ids");
		Integer[] idarr = Helper.str2integerArr(idstr);
		LayuiTableResponse<SysDictEntity> ltr = new LayuiTableResponse<>();
		for (Integer id : idarr) {
			SysDictEntity m = sysDictService.getEntityById(id);
			sysDictService.deleteById(id);
		}
		return ltr;
	}

	@RequestMapping({ "/getChildCount" })
	public LayuiTableResponse<SysDictEntity> getChildCount(Integer id) {
		LayuiTableResponse<SysDictEntity> ltr = new LayuiTableResponse<>();
		Long count = sysDictService.countByParentId(id);
		ltr.setCount(count);

		return ltr;
	}
}
